package ModelLayer;
import java.util.ArrayList;

/**
 * Write a description of class AdressBook here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class AddressBook
{
   private ArrayList<Person> contacts;
   private static AddressBook instance=null;
    /**
     * Constructor
     * Initializa the instance fields
     */
    private AddressBook()
    {
        contacts = new ArrayList<Person>();
    }
    public static AddressBook getInstance()
    {
        if(instance == null)
        {
            instance = new AddressBook();
        }
        return instance;
    }
    /**
 
     */
    public void addContact(Person newContact)
    {
        
        contacts.add(newContact);
    }
    public Person findContact(long search)
    {
        boolean found = false;
        int x = 0;
        while(x < contacts.size() && !found)
        {
            Person s = contacts.get(x);
            if(s.getID() == search)
            {
                found = true;
            }
            else{
                x++;
            }
        }
        if(found){
            return contacts.get(x);
        }
        else{
            return null;
        }
    }
    public void updateContact(long search , long newID , String newName , String newAddress , String newPostalCode , String newCity , String newPhone)
    {

        Person p=findContact(search);
        if(p!=null)
        {
            p.setPhone(newPhone);
            p.setName(newName);
            p.setID(newID);
            p.setAddress(newAddress);
            p.setPostalCode(newPostalCode);
            p.setCity(newCity);
        }
    }
    public void deleteContact(long search)
    {
        Person c=findContact(search);
        if(c!=null)
        {
          contacts.remove(c);
          
        }
    }
    public ArrayList<Person> getList()
    {
        return contacts;
        }
        public int size()
        {
            return contacts.size();
        }
        public Person get(int x)
        {
            return contacts.get(x);
        }
        public int check(long search)
        {
            boolean ok=true;
            Person p=(findContact(search));
            if(p==null)
            {
                return 1;
            }
            else{
                 ok=(p.checkLoan());
                if(ok==true)
               {
                  
                return 2;
               }
                else {
               
                return 3;
               }
            }
        }
        public void addLoan(long id, long loanID, String loanDate, String loanPeriod)
        {
            Person p=findContact(id);
            p.addLoan(loanID, loanDate, loanPeriod);
            
        }
   
    }